package cn.tedu.powerfulfitness.controller;

import cn.tedu.powerfulfitness.pojo.dto.CoachAddNewDTO;
import cn.tedu.powerfulfitness.pojo.dto.CoachUpdateDTO;
import cn.tedu.powerfulfitness.pojo.vo.CoachListItemVO;
import cn.tedu.powerfulfitness.pojo.vo.CoachStandardVO;
import cn.tedu.powerfulfitness.service.ICoachService;
import cn.tedu.powerfulfitness.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/coachs")
@Api(tags = "01.教练管理模块")
@Validated
public class CoachController {

    @Autowired
    private ICoachService coachService;

    @PostMapping("/add-new")
    @ApiOperation("添加教练")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CoachAddNewDTO coachAddNewDTO){
        coachService.addNew(coachAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/coachs/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除教练")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "教练ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "删除相册失败，ID值无效！")
                             @PathVariable Long id) {
        coachService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/coachs/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改教练详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "修改相册详情失败，ID值无效！")
                                     @PathVariable Long id, @Valid CoachUpdateDTO coachUpdateDTO) {
        coachService.updateInfoById(id, coachUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/coachs/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询教练详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "获取相册详情失败，ID值无效！")
                                      @PathVariable Long id) {
        CoachStandardVO queryResult = coachService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9080/coachs
    @GetMapping("")
    @ApiOperation("查询教练列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {
        List<CoachListItemVO> list = coachService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/coachs/upload-icon
    @PostMapping("/upload-icon")
    @ApiOperation("上传图片")
    @ApiOperationSupport(order = 500)
    public JsonResult uploadIcon(MultipartFile picFile){
        String iconPath = coachService.uploadIcon(picFile);
        return JsonResult.ok(iconPath);
    }

    @GetMapping("/remove-icon")
    @ApiOperation("删除图片")
    @ApiOperationSupport(order = 501)
    public JsonResult removeIcon(String url){
        coachService.removeIcon(url);
        return JsonResult.ok();
    }
}
